package com.yyfax.ph.personal.authority.forgot;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.content.Loader;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import com.yyfax.common.form.ClearEditText;
import com.yyfax.common.tools.RegexUtil;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.ph.R;
import com.yyfax.ph.base.BaseFragment;
import com.yyfax.ph.base.YJPH;
import com.yyfax.ph.common.utils.Constants;
import com.yyfax.ph.common.utils.Constants.TimerTask;

public class VerifyCodeFragment extends BaseFragment {
	private static final String TAG = VerifyCodeFragment.class.getSimpleName();
	/**
	 * 手机号号码
	 */
	@BindView(R.id.phone_input_tv)
	EditText mPhoneTv;
	/**
	 * 验证码
	 */
	@BindView(R.id.verify_code_input_edit_tv)
	ClearEditText mPhoneVerifyTv;
	/**
	 * 获取验证码按钮
	 */
	@BindView(R.id.get_verify_tv)
	TextView mGetVerifyTv;
	Unbinder unbinder;

	private ForgotPwdActivity forgotPwdActivity;

	private TimerTask timerTask = new TimerTask() {

		@Override
		public void onTime(int time) {
			mGetVerifyTv.setText(getString(R.string.count_decrease, Constants.TIME - time));
			mGetVerifyTv.setTextColor(getResources().getColor(R.color.color_999999));
			enableBtn(false);
		}

		@Override
		public void onEnd() {
			mGetVerifyTv.setText(getString(R.string.re_get_obtain));
			mGetVerifyTv.setTextColor(getResources().getColor(R.color.color_f05b47));
			enableBtn(true);
		}

		public String getKey() {
			return TAG;
		}
	};
	private String mUserPhone;

	@Override
	public void onAttach(Context ctx) {
		super.onAttach(ctx);
		if (ctx instanceof Activity)
			forgotPwdActivity = (ForgotPwdActivity) ctx;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.find_code_fragment_one_layout_new, null);
		unbinder = ButterKnife.bind(this, view);
		return view;
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		//自动填充手机号码
		mUserPhone = YJPH.getInstance().getPreferenceManager().getUserPhone();
		if (!TextUtils.isEmpty(mUserPhone)) {
			mPhoneTv.setText(mUserPhone);
		}
	}

	@Override
	public void onResume() {
		super.onResume();
		Constants.timerTask = timerTask;
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		Constants.timerTask = null;
	}

	@Override
	public void onDestroyView() {
		super.onDestroyView();
		unbinder.unbind();
	}

	public String getVerifyCode() {
		return mPhoneVerifyTv.getText().toString().trim();
	}

	public void setVerifyCode(String verifyCode) {
		mPhoneVerifyTv.setText(verifyCode);
	}

	public String getCellPhone() {
		return mPhoneTv.getText().toString().trim();
	}

	@OnClick({R.id.next_step_btn, R.id.get_verify_tv})
	public void onClick(View view) {
		switch (view.getId()) {
			case R.id.get_verify_tv:
				if (checkPhoneInput()) {
					forgotPwdActivity.getVeryCode();
				}
				break;
			case R.id.next_step_btn:
				if (checkPhoneInput() && checkVerifyCodeInput()) {
					forgotPwdActivity.checkPhoneCode();
				}
				break;
		}
	}

	private boolean checkPhoneInput() {
		String phoneInput = mPhoneTv.getText().toString().trim();
		if (TextUtils.isEmpty(phoneInput)) {
			showToast(getString(R.string.please_input_toast, getString(R.string.cell_phone_number)));
			return false;
		}

		if (!RegexUtil.isMobileNumber(phoneInput)) {
			showToast(getString(R.string.phone_format_error));
			return false;
		}
		return true;
	}

	private boolean checkVerifyCodeInput() {
		String code = mPhoneVerifyTv.getText().toString().trim();
		if (TextUtils.isEmpty(code)) {
			showToast(getString(R.string.please_input_toast, getString(R.string.verify_code)));
			return false;
		}
		return true;
	}

	public void startCount() {
		Constants.startTimer(TAG);
		if (Constants.timerTask == null)
			Constants.timerTask = timerTask;
	}

	private void enableBtn(boolean enable) {
		mGetVerifyTv.setEnabled(enable);
	}



	@Override
	public void onLoadFinished(Loader<Presenter> loader, Presenter data, int placeholder) {

	}

	@Override
	public Loader<Presenter> onCreateLoader(int id, Bundle args) {
		return null;
	}
}
